#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"
#include "mainwindow.h"
HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //实例化六个界面
    modbusWig = new ModbusWig;
    setWig = new SetWig;
    openCv = new opencv;
    danStm32 = new DanStm32;
    history = new History;
    zhuye1 = new zhuye;

    //将六个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(zhuye1);
    ui->stackedWidget->addWidget(danStm32);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(openCv);
    ui->stackedWidget->addWidget(history);
    ui->stackedWidget->addWidget(setWig);

    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.51.214"));//原127.0.0.1
    mqtt->setPort(1883);//原1883
    mqtt->connectToHost();

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息   
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)), modbusWig, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)), danStm32, SLOT(dataUpSlot(QByteArray)));
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)), history, SLOT(dataUpSlot(QByteArray)));
}

HomePage::~HomePage()
{
    delete ui;
}
